﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace CDSysLDManageProject
{
    public partial class FmArgSetting : Form
    {
        public FmArgSetting()
        {
            InitializeComponent();
        }
        int allowocv;
        int allowdcir;
        string ocvhigh;
        string ocvlow;
        public string CheckStatus = "";
        private void FmArgSetting_Load(object sender, EventArgs e)
        {
            CreateArgXmlFile();
            if (allowocv == 1)
            {
                cbxallowocv.Checked = true;
            }
            else
            {
                cbxallowocv.Checked = false;
            }

            if (allowdcir == 1)
            {
                cbxallowdcir.Checked = true;
            }
            else
            {
                cbxallowdcir.Checked = false;
            }
        }


        //创建或获取XML的指定的地址
        public void CreateArgXmlFile()
        {



            XmlDocument xmlDoc = new XmlDocument();
            
            string XMLPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            XMLPath = XMLPath + "\\" + "ArugmentSetting.xml";
            //如果不存在导出路径XML的话则创建。
            if (!File.Exists(XMLPath))
            {
                //创建类型声明节点  
                XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
                xmlDoc.AppendChild(node);
                //创建根节点  

                XmlNode root = xmlDoc.CreateElement("参数");
                xmlDoc.AppendChild(root);
                //string SysPath = ArgSetPath;
                CreateNode(xmlDoc, root, "允许OCV测试", "1");
                CreateNode(xmlDoc, root, "允许DCIR测试", "1");
                CreateNode(xmlDoc, root, "OCV下限", "1");
                CreateNode(xmlDoc, root, "OCV上限", "3");
               
                try
                {
                    XMLPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                    XMLPath = XMLPath + "\\" + "ArugmentSetting.xml";
                    xmlDoc.Save(XMLPath);
                    return;
                }
                catch (Exception e)
                {
                    //显示错误信息  
                    Console.WriteLine(e.Message);
                }

            }
            else
            {
                //XMLPath = XMLPath + "\\" + "ArugmentSetting.xml";
                xmlDoc.Load(XMLPath);
                //获取某个节点
                XmlNode allowocv1 = xmlDoc.SelectSingleNode("参数").SelectSingleNode("允许OCV测试");
                XmlNode allowdcir1 = xmlDoc.SelectSingleNode("参数").SelectSingleNode("允许DCIR测试");
                XmlNode ocvlow1 = xmlDoc.SelectSingleNode("参数").SelectSingleNode("OCV下限");
                XmlNode ocvhigh1 = xmlDoc.SelectSingleNode("参数").SelectSingleNode("OCV上限");
               

                //判断OCV,ACIR,TEMP,CASEV  NG是否选中
                allowocv = int.Parse(allowocv1.InnerText);
                allowdcir = int.Parse(allowdcir1.InnerText);
                ocvlow = ocvlow1.InnerText;
                ocvhigh = ocvhigh1.InnerText;
                tbxocvhigh.Text = ocvhigh.ToString();
                tbxocvlow.Text = ocvlow.ToString();
                
            }
            return;
        }

        public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
        {
            XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
            node.InnerText = value;
            parentNode.AppendChild(node);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string flag = "argset";
            FormClosedCheck formcheck = new FormClosedCheck(flag);
            formcheck.Owner = this;
            formcheck.ShowDialog();

            if (CheckStatus != "OK")
            {
                MessageBox.Show("请输入正确密码!");
                return;
            }

            XmlDocument xmlDoc = new XmlDocument();

            string XMLPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            XMLPath = XMLPath + "\\" + "ArugmentSetting.xml";
            //如果不存在导出路径XML的话则创建。
            if (!File.Exists(XMLPath))
            {
                //创建类型声明节点  
                XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
                xmlDoc.AppendChild(node);
                //创建根节点  

                XmlNode root = xmlDoc.CreateElement("参数");
                xmlDoc.AppendChild(root);
                //string SysPath = ArgSetPath;
                CreateNode(xmlDoc, root, "允许OCV测试", "1");
                CreateNode(xmlDoc, root, "允许DCIR测试", "1");
                CreateNode(xmlDoc, root, "OCV下限", "1");
                CreateNode(xmlDoc, root, "OCV上限", "3");

                try
                {
                    XMLPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                    XMLPath = XMLPath + "\\" + "ArugmentSetting.xml";
                    xmlDoc.Save(XMLPath);
                    this.Close();
                }
                catch (Exception ex)
                {
                    //显示错误信息  
                    Console.WriteLine(ex.Message);
                }

            }
            else
            {
                //XMLPath = XMLPath + "\\" + "ArugmentSetting.xml";
                xmlDoc.Load(XMLPath);
                //获取某个节点
                XmlNode allowocv1 = xmlDoc.SelectSingleNode("参数").SelectSingleNode("允许OCV测试");
                XmlNode allowdcir1 = xmlDoc.SelectSingleNode("参数").SelectSingleNode("允许DCIR测试");
                XmlNode ocvlow1 = xmlDoc.SelectSingleNode("参数").SelectSingleNode("OCV下限");
                XmlNode ocvhigh1 = xmlDoc.SelectSingleNode("参数").SelectSingleNode("OCV上限");
                ocvlow1.InnerText = tbxocvlow.Text;
                ocvhigh1.InnerText = tbxocvhigh.Text;
                //判断OCV,DCIR是否选中
                if (cbxallowocv.Checked == true)
                {
                    allowocv1.InnerText="1";
                }
                else
                {
                    allowocv1.InnerText = "0";
                }

                if (cbxallowdcir.Checked == true)
                {
                    allowdcir1.InnerText="1";
                }
                else
                {
                    allowdcir1.InnerText = "0";
                }

                try
                {
                    XMLPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                    XMLPath = XMLPath + "\\" + "ArugmentSetting.xml";
                    xmlDoc.Save(XMLPath);

                }
                catch (Exception exp)
                {
                    //显示错误信息  
                    Console.WriteLine(exp.Message);
                }
                
            }
            return;
        }


    }
}
